﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ComputerStoreManager;
using Modules.Core;
using Modules.Library.Entities;

namespace ComputerStoreManager.ViewModel {

    //public delegate void OnViewInvoiceBuyPreviewHandler();
    //public delegate void OnViewInvoiceBuyNextHander();
    //public delegate void OnViewInvoiceBuyPrintInvoiceHandler();
    public delegate void OnViewInvoiceBuySearchHandler();
    public delegate void OnViewInvoiceBuyShowAllHandler();
    public class ViewInvoiceBuyControlViewModel : BaseViewModel {
        #region ICommand
        public ICommand PreviewCommand {
            get;
            private set;
        }
        public ICommand NextCommand {
            get;
            private set;
        }
        public ICommand PrintInvoiceCommand {
            get;
            private set;
        }
        public ICommand SearchCommand {
            get;
            private set;
        }
        public ICommand ShowAllCommand {
            get;
            private set;
        }
        #endregion

        #region Events
        //public OnViewInvoiceBuyPreviewHandler OnPreviewClicked;
        //public OnViewInvoiceBuyNextHander OnNextClicked;
        //public OnViewInvoiceBuyPrintInvoiceHandler OnPrintInvoiceClicked;
        public OnViewInvoiceBuySearchHandler OnSearchClicked;
        public OnViewInvoiceBuyShowAllHandler OnShowAllClicked;

        //private TextBox txtAmount;
        //private TextBox txtBonusId;
        //private TextBox txtDate;
        //private TextBox txtDetailsInvoiceId;
        //private TextBox txtInvoiceId;
        //private TextBox txtPrice;
        //private TextBox txtProductId;
        private TextBox txtSearch;
        private ComboBox cbbSearchBy;
        //private Button btnNext;
        //private Button btnPreview;
        //private Button btnPrintInvoice;
        private Button btnSearch;
        private Button btnShowAll;
        private DataGrid dataGrid1;
        //private DataTable dataTemp;
        //private int counter;

        public ViewInvoiceBuyControlViewModel(TextBox txtSearch, ComboBox cbbSearchBy, Button btnSearch, Button btnShowAll, DataGrid dataGrid1) {
            // TODO: Complete member initialization
            this.txtSearch = txtSearch;
            this.cbbSearchBy = cbbSearchBy;
            this.btnSearch = btnSearch;
            this.btnShowAll = btnShowAll;
            this.dataGrid1 = dataGrid1;

            InitCmd();
        }
        #endregion

        public void InitCmd() {
            //PreviewCommand = new ExecutableCommand(DoPreview);
            //NextCommand = new ExecutableCommand(DoNext);
            PrintInvoiceCommand = new ExecutableCommand(DoPrintInvoice);
            SearchCommand = new ExecutableCommand(DoSearch);
            ShowAllCommand = new ExecutableCommand(DoShowAll);
        }

        private void DoPrintInvoice(object obj) {
            //throw new NotImplementedException();
        }

        private void DoSearch() {
            try {
                var data = Model.SearchInfo(txtSearch.Text, (SearchingCondition)cbbSearchBy.SelectedIndex, KindOfObject.BY_INVOICE);
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác" + ex.Message, "Thong Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void DoShowAll() {
            try {
                var data = Model.GetListHoaDon();
                if (data != null && data.Rows.Count > 0) {
                    dataGrid1.ItemsSource = data.DefaultView;
                }
                else {
                    MessageBox.Show("Không tìm thấy dữ liệu", "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            catch (Exception ex) {
                MessageBox.Show("Lỗi thao tác" + ex.Message, "Thông Báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        //public void DoPreview()
        //{
        //    Preview();
        //}

        //public void DoNext()
        //{
        //    Next();
        //}

        //private void LoadData()
        //{
        //    Connection con = new Connection();
        //    dataTemp = con.GetDataTable("select * from invoiceresultview");
        //    counter = 0;
        //}

        //private void Next()
        //{
        //    if (counter >= dataTemp.Rows.Count)
        //        counter = dataTemp.Rows.Count;
        //    else
        //        counter++;
        //    SetDataToComponents();
        //}

        //private void Preview()
        //{
        //    if (counter <= 0)
        //        counter = 0;
        //    else
        //        counter--;
        //    SetDataToComponents();
        //}

        //private void SetDataToComponents()
        //{
        //}

        //private bool CheckIsNotNull()
        //{
        //    return
        //        txtAmount.Text != " " &&
        //        txtBonusId.Text != " " &&
        //        txtDate.Text != " " &&
        //        txtDetailsInvoiceId.Text != " " &&
        //        txtInvoiceId.Text != " " &&
        //        txtPrice.Text != " " &&
        //        txtProductId.Text != " ";
        //}
    }
}
